package com.vt.common.core.util;

import com.vt.common.core.exception.BusinessException;
import lombok.Getter;
import lombok.Setter;
import lombok.ToString;

import java.io.Serializable;

/**
 * 响应信息主体
 */
@ToString
public class R<T> implements Serializable {
    private static final int SUCCESS = 0;
    private static final int FAIL = 1;
    private static final long serialVersionUID = 1L;
    @Getter
    @Setter
    private String msg = "success";

    @Getter
    @Setter
    private int status = SUCCESS;

    @Getter
    @Setter
    private T data;

    public R() {
        super();
    }

    public R(T data) {
        super();
        this.data = data;
    }

    public R(T data, String msg) {
        super();
        this.data = data;
        this.msg = msg;
    }

    public R(Integer status, String msg) {
        super();
        this.status = status;
        this.msg = msg;
    }

    public R(Throwable e) {
        super();
        if (e instanceof BusinessException) {
            this.msg = e.getMessage();
        } else {
            this.msg = "系统异常,请联系系统管理员";
        }
        this.status = FAIL;
    }
}
